{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## generate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "选择午餐取决于您的口味偏好、饮食限制和所在地区的餐饮选项。这里有几个建议：\n",
      "\n",
      "1. **中式快餐**：比如炒面、盖浇饭或是各种便当，这些都是快速又方便的选择。\n",
      "2. **健康轻食**：沙拉、寿司卷或是一些蔬菜三明治都是不错的选择，适合追求健康饮食的人群。\n",
      "3. **地方特色小吃**：如果有机会的话，不妨尝试一下当地的特色小吃，既可以满足味蕾又能了解当地文化。\n",
      "4. **素食料理**：如果您是素食主义者或是想要吃得更健康一些，可以选择素菜或豆制品为主的餐点。\n",
      "\n",
      "当然，还有许多其他选项可以根据个人喜好来决定。希望这些建议能够帮助到您！\n"
     ]
    }
   ],
   "source": [
    "import ollama\n",
    "client = ollama.Client(host='http://192.168.20.43:11434')\n",
    "\n",
    "options = {\n",
    "    \"num_ctx\": 32768  # 设置 num_ctx 参数\n",
    "}\n",
    "\n",
    "response = client.generate(\n",
    "    model='qwen2.5:14b', \n",
    "    prompt='今天中午吃什么好？',\n",
    "    options = options\n",
    ")\n",
    "#print(response)\n",
    "print(response['response'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 流式响应\n",
    "\n",
    "可以通过设置 stream=True 启用响应流，使函数调用返回一个 Python 生成器，其中每个部分都是流中的一个对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "选择午餐取决于你的口味偏好、饮食限制以及可用的食材或餐厅选项。这里有几个不同风格和类型的建议，希望你能找到喜欢的选择：\n",
      "\n",
      "1. **中式快餐**：可以考虑去附近的中餐馆或者小吃店点一份盖浇饭、炒面或者是牛肉拉面。\n",
      "2. **健康轻食**：沙拉是个不错的选择，可以选择含有各种蔬菜的混合沙拉，再加一些鸡胸肉或豆制品增加蛋白质摄入量。\n",
      "3. **日式料理**：寿司和刺身是很多人喜爱的选项。如果你不吃生鱼片，也可以尝试便当盒饭或是乌冬面。\n",
      "4. **西式快餐**：汉堡、薯条或者是鸡肉三明治也很受欢迎。记得搭配一些蔬菜沙拉或水果作为补充。\n",
      "5. **素食选择**：很多餐馆都提供素食菜单，比如素炒菜、豆腐料理等。\n",
      "\n",
      "当然，如果条件允许的话，自己动手做一顿健康的午餐也是一个不错的选择。可以根据冰箱里现有的食材来决定做什么菜，既环保又经济实惠。希望这些建议对你有所帮助！"
     ]
    }
   ],
   "source": [
    "import ollama\n",
    "client = ollama.Client(host='http://192.168.20.43:11434')\n",
    "\n",
    "stream = client.generate(\n",
    "    model='qwen2.5:14b', \n",
    "    stream=True,\n",
    "    prompt='今天中午吃什么好？',\n",
    ")\n",
    "\n",
    "# 流式输出\n",
    "for chunk in stream:\n",
    "  print(chunk['response'], end='', flush=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
